﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LCARSLibrary
{
    public static class Utilities
    {
        public static List<string> GetOneLevelDirectories(string path)
        {
            return GetOneLevelDirectoriesRec(path, null);
        }

        public static List<string> GetOneLevelDirectories(string path, List<string> exceptDirNames)
        {
            List<string> lowerExceptDirNames = null;

            if (exceptDirNames != null)
            {
                lowerExceptDirNames = new List<string>();

                foreach (var item in exceptDirNames)
                    lowerExceptDirNames.Add(item.ToLower());
            }

            return GetOneLevelDirectoriesRec(path, lowerExceptDirNames);
        }

        private static List<string> GetOneLevelDirectoriesRec(string path, List<string> exceptDirNames)
        {
            List<string> result = new List<string>();

            string[] dirs = Directory.GetDirectories(path);

            foreach (var dir in dirs)
            {
                try
                {
                    string[] subDirs = Directory.GetDirectories(dir);

                    List<string> filteredSubDirs;

                    if (exceptDirNames != null)
                    {
                        var d = from x in subDirs
                                where !exceptDirNames.Contains(Path.GetFileName(x).ToLower())
                                select x;

                        filteredSubDirs = d.ToList();
                    }
                    else
                    {
                        filteredSubDirs = subDirs.ToList();
                    }

                    //add the parent dir too
                    result.Add(dir);

                    if (filteredSubDirs.Count() > 0)
                        result.AddRange(GetOneLevelDirectoriesRec(dir, exceptDirNames));

                    //if (filteredSubDirs.Count() > 0)
                    //    result.AddRange(GetOneLevelDirectoriesRec(dir, exceptDirNames));
                    //else
                    //    result.Add(dir);
                }
                catch
                {
                }
            }

            return result;
        }

        public static string ReplaceSpecialXMLSymbols(string input)
        {
            input = input.Replace("&", "&amp;");
            input = input.Replace("'", "&apos;");
            input = input.Replace("<", "&lt;");
            input = input.Replace(">", "&gt;");
            input = input.Replace("\"", "&quot;");
            input = input.Replace("Ä", "&#196;");
            input = input.Replace("Ö", "&#214;");
            input = input.Replace("Ü", "&#220;");
            input = input.Replace("ä", "&#228;");
            input = input.Replace("ö", "&#246;");
            input = input.Replace("ü", "&#252;");
            input = input.Replace("ß", "&#223;");

            return input;
        }
    }
}
